package com.romotc.qqrobot.webqq.qqInterfaceLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result_Value;
import com.romotc.qqrobot.webqq.qqInterfaceCall.Get_cface2_InterCall;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Get_file2_processor;
import com.romotc.scache.RequestSpy;

/**
 * 保存消息中的图片
 * @author Administrator
 *
 */
public class Poll2_Cface_Saver_Log extends Poll2_Processor_Log {

	
	@Override
	public boolean checkSkipLog(RequestSpy rqSpy, QQUrlWatcherConfig config) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 防止接受两遍文件
	 */
	private static Map<String, Boolean> transFiles 
		= new HashMap<String, Boolean>();
		
	public synchronized static boolean getTransFile(String picName) {
		Boolean b = transFiles.get(picName);
		return b == null ? false : b;
	}
	public synchronized static void setTransFile(String picName) {
		transFiles.put(picName, true);
	}
	
	@Override
	String getPollType() {
		return Poll2_Processor_Log.Key_Message;
	}

	@Override
	void processPollEmpty(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody response) {
		// TODO Auto-generated method stub

	}

	@Override
	void processPollResult(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody_Result result, Poll2_ResponseBody response,
			int resultIdx) {
		/**
		 * {"retcode":0,"result":
		 * 		[{"poll_type":"message",
		 * 			"value":
		 * 				{"msg_id":15155,"from_uin":1562444454,
		 * 				"to_uin":1586030808,"msg_id2":490405,"msg_type":9,
		 * 				"reply_ip":176752342,"time":1327043011,
		 * 				"content":[
		 * 					["font",{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}],
		 * 					["cface","C98C5D9B33306040DBAEA163D4BDF147.jpg",""],
		 * 					["cface","73A1029B6C7F8D351DAA6D10F9EB2FBB.jpg",""],
		 * 				" "]}}]}
		 */
		JSONObject reqData = new JSONObject();
		Poll2_ResponseBody_Result_Value value = result.getValue();
		if(value == null)
			return;
		
		List<List<String>> cfaces = (List)value.filterContent(Poll2_ResponseBody_Result_Value.Key_Content_cface);
		if(cfaces == null || cfaces.size() == 0) 
			return;
		
		try {
			reqData.put(Get_cface2_InterCall.Key_Input_Must_senderUin, value.getFrom_uin());
			reqData.put(Get_cface2_InterCall.Key_Must_Input_Lcid, value.getMsg_id());
			
			Get_cface2_InterCall cfaceCall = Get_cface2_InterCall.getInstance();
			QQLogedin curQqLogedIn = QQLogedin.getCurQqLogedIn(rqSpy);
			for(List<String> cface : cfaces) {
				String picName = cface.get(1);
				if(StringUtils.isNotBlank(picName) && !getTransFile(picName)) {
					reqData.put(Get_cface2_InterCall.Key_Must_Input_FileName, picName);
					setTransFile(picName);
					cfaceCall.call(curQqLogedIn, reqData, Get_file2_processor.getInstance());
				}
			}
			System.out.println();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
